/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOOracle;

import DAO.DAODomicilio;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import modelo.Domicilio;
import modelo.Paciente;

/**
 *
 * @author Nai
 */
public class DomicilioDAOOracle implements DAODomicilio {

    public DomicilioDAOOracle() {
    }

    public boolean insert(Domicilio dire) {
        Connection con = OracleDAOFactory.createConnection();
        boolean res = false;
        try {
            PreparedStatement st = con.prepareStatement("insert into domicilios " +
                    "(CALLE,NUMERO,PISO,DEPTO,REFERENCIA,BARRIO,LOCALIDAD,PROVINCIA,USUARIO_ID) " +
                    "values (?,?,?,?,?,?,?,?,?)");
            st.setString(1, dire.getCalle());
            st.setInt(2, dire.getNumero());
            st.setInt(3, dire.getPiso());
            st.setString(4, dire.getDepto());
            st.setString(5, dire.getReferencia());
            st.setString(6, dire.getBarrio());
            st.setString(7, dire.getLocalidad());
            st.setString(8, dire.getProvincia());
            st.setInt(9, dire.getUsuarioID());

            res = st.execute();
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return res;
    }

    public boolean delete(Domicilio dire) {
        Connection con = OracleDAOFactory.createConnection();
        boolean res = false;
        try {
            PreparedStatement st = con.prepareStatement("delete from domicilios where ID_DOMICILIO=?");
            st.setInt(1, dire.getIdDomicilio());
            st.execute();
            con.close();
            res = true;

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            res = false;
        }
        return res;
    }

    public boolean delete(Paciente pac) {
        Connection con = OracleDAOFactory.createConnection();
        boolean res = false;
        try {
            PreparedStatement st = con.prepareStatement("delete from domicilios where USUARIO_ID=?");
            st.setInt(1, pac.getIdPaciente());
            st.execute();
            con.close();
            res = true;

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            res = false;
        }
        return res;
    }

    public boolean update(Domicilio dire) {
        Connection con = OracleDAOFactory.createConnection();
        boolean res = false;
        try {
            PreparedStatement st = con.prepareStatement("update DOMICILIOS set " +
                    "CALLE=?,NUMERO=?,PISO=?,DEPTO=?,REFERENCIA=?,BARRIO=?,LOCALIDAD=?,PROVINCIA=? " +
                    "where id_domicilio=?");

            st.setString(1, dire.getCalle());
            st.setInt(1, dire.getNumero());
            st.setInt(3, dire.getPiso());
            st.setString(4, dire.getDepto());
            st.setString(5, dire.getReferencia());
            st.setString(6, dire.getBarrio());
            st.setString(7, dire.getLocalidad());
            st.setString(8, dire.getProvincia());
            st.setInt(9, dire.getIdDomicilio());
            res = st.execute();
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return res;
    }

    public Domicilio find(Domicilio dire) {
        Connection con = OracleDAOFactory.createConnection();
        Domicilio dir = null;

        try {//(ID_DOMICILIO,CALLE,NUMERO,PISO,DEPTO,REFERENCIA,BARRIO,LOCALIDAD,PROVINCIA)
            ResultSet rs = con.createStatement().executeQuery("select * from domicilios where id_domicilio="
                    + dire.getIdDomicilio());
            if (rs.next()) {
                dir = new Domicilio();
                dir.setIdDomicilio(rs.getInt(1));
                dir.setCalle(rs.getString(2));
                dir.setNumero(rs.getInt(3));
                dir.setPiso(rs.getInt(4));
                dir.setDepto(rs.getString(5));
                dir.setReferencia(rs.getString(6));
                dir.setBarrio(rs.getString(7));
                dir.setLocalidad(rs.getString(8));
                dir.setProvincia(rs.getString(9));
                dir.setUsuarioID(rs.getInt(10));
            }
            con.close();
        } catch (SQLException ex) {
        }
        return dir;
    }

    public Collection<Domicilio> list(int id_usuario) {
        Connection con = OracleDAOFactory.createConnection();
        LinkedList<Domicilio> res = new LinkedList();

        try {//(ID_DOMICILIO,CALLE,NUMERO,PISO,DEPTO,REFERENCIA,BARRIO,LOCALIDAD,PROVINCIA)
            ResultSet rs = con.createStatement().executeQuery("select * from domicilios where usuario_id=" + id_usuario);
            while (rs.next()) {
                Domicilio dir = new Domicilio();
                dir.setIdDomicilio(rs.getInt(1));
                dir.setCalle(rs.getString(2));
                dir.setNumero(rs.getInt(3));
                dir.setPiso(rs.getInt(4));
                dir.setDepto(rs.getString(5));
                dir.setReferencia(rs.getString(6));
                dir.setBarrio(rs.getString(7));
                dir.setLocalidad(rs.getString(8));
                dir.setProvincia(rs.getString(9));
                dir.setUsuarioID(rs.getInt(10));
                res.add(dir);
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return res;
    }
    
    
    public int ultimoReg (){
        Connection con = OracleDAOFactory.createConnection();
        int dir = 0;
        try {//(ID_DOMICILIO,CALLE,NUMERO,PISO,DEPTO,REFERENCIA,BARRIO,LOCALIDAD,PROVINCIA)
            ResultSet rs = con.createStatement().executeQuery("SELECT MAX(id_domicilio) FROM domicilios");
            if (rs.next()) {
                dir= rs.getInt(1);
                
            }
            con.close();
        } catch (SQLException ex) {
        }
        return dir;
    }
    
}
